<template>
    <div>
        <div class="add">
            <div></div>
            <el-button class="add-button" type="text"
                @click="dialogVisible = true, flag = true, title = '添加'">增加</el-button>
        </div>
        <div class="tab">
            <div class="custom-tree-container">
                <div class="block">
                    <el-tree :data="list" node-key="id" default-expand-all ref="tree" @node-click="handleNodeClick">
                        <span class="custom-tree-node" slot-scope="{node}">
                            <span>{{ node.label }}</span>
                            <span>
                                <el-button
                                    style="font-size: 16px; padding: 10px 20px; border-radius: 0; border: 1px solid #666; background: #fff;color: #666; font-size: 16px;"
                                    class="elbtn" type="text" size="mini" @click=remove(node.data.id)>
                                    删除
                                </el-button>
                                <el-button
                                    style=" font-size: 16px; padding: 10px 20px;border-radius: 0; background: #08cfde;color: #000000;"
                                    class="elbtn" type="text" size="mini" @click=modify(node.data)>
                                    修改
                                </el-button>
                                <el-button
                                    style=" font-size: 16px; padding: 10px 20px;border-radius: 0; background: #08cfde;color: #000000;"
                                    :disabled="isButtonDisabled(node)" class="elbtn" type="text" size="mini"
                                    @click=append(node)>
                                    增加
                                </el-button>
                            </span>
                        </span>
                    </el-tree>
                </div>
            </div>
        </div>
        <!-- dialog对话框 -->
        <el-dialog :title="title" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
            <el-form label-position="left" label-width="70px">
                <el-form-item label="标题">
                    <el-input v-model="data.title"></el-input>
                </el-form-item>
                <el-form-item label="类型">
                    <el-select v-model="data.type" placeholder="请选择">
                        <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button
                    style="font-size: 16px; padding: 10px 20px; border-radius: 0; border: 1px solid #666; background: #fff;color: #666; font-size: 16px;"
                    @click="cancellation">取 消</el-button>
                <el-button
                    style=" font-size: 16px; border-color: #08cfde; padding: 10px 20px;border-radius: 0; background: #08cfde;color: #000000;"
                    type="primary" @click="add">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>
<script>
import { getIndustryList, addIndustry, delIndustry, updateIndustry } from "@/api/Industry"
export default {
    data() {
        return {
            title: "",
            flag: true,
            data: {
                title: "",
                before_id: 0,
                type: null,
                id: 0
            },
            list: [],
            dialogVisible: false,//对话框是否显示
            options: [
                {
                    value: 0,
                    label: '无'
                },
                {
                    value: 1,
                    label: '详情'
                }, {
                    value: 2,
                    label: '列表'
                }
            ]
        }
    },
    created() {
        this.getIndustry()
    },
    methods: {
        cancellation() {
            this.dialogVisible = false
            this.data = {
                title: "",
                before_id: 0,
                type: null,
                id: 0
            }
        },
        isButtonDisabled(node) {
            return node.data.type !== 0 && node.data.before_id !== 0;

        },
        async getIndustry() {
            try {
                const res = await getIndustryList().catch(() => {
                    this.$message({
                        message: '网络不给力呦',
                        type: 'warning'
                    });
                })
                // console.log(res, 666);
                this.list = res.data.data.map(item => {
                    return {
                        before_id: item.before_id,
                        id: item.id,
                        label: item.title,
                        type: item.type,
                        children: item.list.map(itemx => {
                            return {
                                before_id: itemx.before_id,
                                type: itemx.type,
                                id: itemx.id,
                                label: itemx.title
                            };
                        })
                    };
                });
                console.log(this.list, 'list');
            } catch (error) {
                console.error("Error fetching industry list:", error);
            }
        },
        append(node) {
            this.title = '添加'
            this.dialogVisible = true
            this.flag = true
            this.data.before_id = node.data.id
        },
        // addIndustry(this.data).then(
        //             this.getIndustry().then(
        //                 this.$message({
        //                     type: 'success',
        //                     message: '添加成功!'
        //                 }),
        //                 this.dialogVisible = false,
        //                 this.data = {
        //                     title: "",
        //                     before_id: 0,
        //                     type: null
        //                 }
        //             )

        //         )
        add() {
            if (this.flag) {

                // 添加
                if (this.data.type > 0 && this.data.title) {
                    console.log(this.data.before_id);
                    addIndustry(this.data).then(() => {

                        this.getIndustry()
                        this.$message({
                            type: 'success',
                            message: '添加成功!'
                        }),
                            this.dialogVisible = false,
                            this.data = {
                                title: "",
                                before_id: 0,
                                type: null
                            }
                    }


                    ).catch(() => {
                        this.$message({
                            message: '网络不给力呦',
                            type: 'warning'
                        });
                    })
                } else {
                    this.$message({
                        type: 'warning',
                        message: '类目和标题不能为空'
                    })
                }
            } else {
                // 编辑
                updateIndustry(this.data).then(arr => {
                    if (arr.data.msg) {
                        this.$message.error(arr.data.msg)
                        this.dialogVisible = false,
                            this.data = {
                                title: "",
                                before_id: 0,
                                type: null
                            }
                    } else {
                        this.getIndustry().then(
                            this.$message({
                                type: 'success',
                                message: '修改成功!'
                            }),
                            this.dialogVisible = false,
                            this.data = {
                                title: "",
                                before_id: 0,
                                type: null
                            }).catch(() => {
                                this.$message({
                                    message: '网络不给力呦',
                                    type: 'warning'
                                });
                            })
                    }
                }

                ).catch(() => {
                    this.$message({
                        message: '网络不给力呦',
                        type: 'warning'
                    });
                })
            }

        },
        remove(id) {
            // 删除数据
            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                delIndustry(id).then((arr) => {
                    console.log(arr, 666);
                    if (arr.data.msg) {
                        this.$message.error(arr.data.msg)
                        this.dialogVisible = false,
                            this.data = {
                                title: "",
                                before_id: 0,
                                type: null
                            }
                    } else {
                        this.getIndustry()
                        this.$message({
                            type: 'success',
                            message: '删除成功!'
                        });
                    }
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        modify(data) {
            this.title = "编辑"
            this.flag = false
            console.log(data, 234);
            this.data = {
                title: data.label,
                id: data.id,
                before_id: data.before_id,
                type: data.type === 1 ? "详情" : data.type === 2 ? "列表" : '无'
            }
            console.log(this.data, 432);
            this.dialogVisible = true
        },
        handleNodeClick(data, node) {
            if (!data.children || data.children.length === 0) {
                // 如果点击的节点没有子节点
                const tree = this.$refs.tree;
                const parentNode = node.parent;

                // 关闭上一个展开的子节点
                if (parentNode) {
                    tree.$emit('node-toggle', parentNode.data, false);
                }
            }

        },
        handleClose(done) {
            this.$confirm('确认关闭？').then(() => {
                done();
            }).catch(() => { });
        }

    }
}
</script>
<style scoped lang="less">
.custom-tree-node {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 18px;
    padding-right: 8px;
}

/deep/.el-tree-node__content {
    height: 60px;
}

.custom-tree-container {
    background-color: #fff;

    .block {
        padding-top: 50px;
    }
}

.add {
    background-color: #fff;
    display: flex;
    justify-content: space-between;

    .add-button {
        font-size: 16px;
        color: #000000;
        position: absolute;
        top: 70px;
        right: 30px;
        z-index: 1;
        background-color: #08cfde;
        border: none;
        border-radius: 0;
        // padding: 0 10px;
        padding-left: 20px;
        padding-right: 20px;
    }
}

.tab {
    margin: 10px;
    padding: 10px;
    background-color: #fff;
}

.elbtn {
    font-size: 18px;
}
</style>